package com.chen.blue.oa.handler;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.chen.blue.dictionary.AuditDict;
import com.chen.blue.dictionary.SystemInnerDict;
import com.chen.blue.domain.model.business.ContestApplyActiveModel;
import com.chen.blue.domain.model.business.ContestApplyRecordModel;
import com.chen.blue.exception.e.DtoNullException;
import com.chen.blue.oa.AbstractOaRequestHandler;
import com.chen.blue.oa.OaRequest;
import lombok.extern.slf4j.Slf4j;

/**
 * 教务处经办人
 * Created By CLT On 2024/5/29.
 */
@SuppressWarnings("all")
@Slf4j
public class OfficeManagerHandler extends AbstractOaRequestHandler {

    public OfficeManagerHandler() {
        setRole(SystemInnerDict.ROLE_INNER_OFFICE_MANAGER_CODE);
    }

    @Override
    public Boolean handlerRquest(OaRequest request) throws DtoNullException {

        if (!(request.getRoleCodeList().contains(super.role)))
            return super.handler.handlerRquest(request);

        Long id = request.getId();
        Integer auditStatus = request.getAuditStatus();
        UpdateWrapper<ContestApplyActiveModel> wrapper = new UpdateWrapper<>();

        if (AuditDict.AUDIT_STATUS_PASS.equals(auditStatus)) {

            ContestApplyRecordModel lastVersion = getLastVersion(id);
            setStatusAndVersion(AuditDict.STAGE_STAGE_FIRST, AuditDict.ROLE_INNER_OFFICE_MANAGER_CODE_ID, AuditDict.AUDIT_STATUS_PASS, AuditDict.CURRENT_STATUS_NOT_AUDIT, lastVersion);
            boolean b1 = iContestApplyRecordService.save(lastVersion);
            if (!b1) {
                log.warn("记录失败");
            } else {
                if (!recordResourceInfoHistory(id, lastVersion)) log.warn("资源记录失败");
            }

            setUpdateWrapper(wrapper, id, AuditDict.STAGE_STAGE_FIRST, AuditDict.ROLE_INNER_OFFICE_CHARGE_CODE_ID, AuditDict.AUDIT_STATUS_NO, AuditDict.CURRENT_STATUS_NOT_AUDIT);

            // 防止跨级跨阶段审核
            super.handlerRquest(request);

            return iContestApplyActiveService.update(wrapper);

        } else if (AuditDict.AUDIT_STATUS_NOT_PASS.equals(auditStatus)) {

            ContestApplyRecordModel lastVersion = getLastVersion(id);

            setStatusAndVersion(AuditDict.STAGE_STAGE_FIRST, AuditDict.ROLE_INNER_OFFICE_MANAGER_CODE_ID, AuditDict.AUDIT_STATUS_NOT_PASS, AuditDict.CURRENT_STATUS_NOT_PASS, lastVersion);
            // 停止流程
            lastVersion.setIsActive(Boolean.FALSE);
            boolean b1 = iContestApplyRecordService.save(lastVersion);
            if (!b1) {
                log.warn("记录失败");
            } else {
                if (!recordResourceInfoHistory(id, lastVersion)) log.warn("资源记录失败");
            }

            setUpdateWrapper(wrapper, id, AuditDict.STAGE_STAGE_FIRST, AuditDict.ROLE_INNER_NO, AuditDict.AUDIT_STATUS_NO, AuditDict.CURRENT_STATUS_NOT_REPORT);

            // 防止跨级跨阶段审核
            super.handlerRquest(request);

            return iContestApplyActiveService.update(wrapper);

        }


        return Boolean.FALSE;

    }

    @Override
    public Boolean handlerRequestPersonInfo(OaRequest request) throws DtoNullException {

        if (!(request.getRoleCodeList().contains(super.role)))
            return super.handler.handlerRequestPersonInfo(request);

        Long id = request.getId();
        Integer auditStatus = request.getAuditStatus();
        UpdateWrapper<ContestApplyActiveModel> wrapper = new UpdateWrapper<>();

        if (AuditDict.AUDIT_STATUS_PASS.equals(auditStatus)) {

            ContestApplyRecordModel lastVersion = getLastVersion(id);
            setStatusAndVersion(AuditDict.STAGE_STAGE_SECOND, AuditDict.ROLE_INNER_OFFICE_MANAGER_CODE_ID, AuditDict.AUDIT_STATUS_PASS, AuditDict.CURRENT_STATUS_NOT_AUDIT, lastVersion);
            boolean b1 = iContestApplyRecordService.save(lastVersion);
            if (!b1) {
                log.warn("记录失败");
            } else {
                // 记录历史人员信息
                if (!recordPersonInfoHistory(id, lastVersion)) log.warn("记录历史人员信息失败");
            }

            setUpdateWrapper(wrapper, id, AuditDict.STAGE_STAGE_SECOND, AuditDict.ROLE_INNER_OFFICE_CHARGE_CODE_ID, AuditDict.AUDIT_STATUS_NO, AuditDict.CURRENT_STATUS_NOT_AUDIT);

            // 防止跨级跨阶段审核
            super.handlerRequestPersonInfo(request);

            return iContestApplyActiveService.update(wrapper);

        } else if (AuditDict.AUDIT_STATUS_NOT_PASS.equals(auditStatus)) {

            ContestApplyRecordModel lastVersion = getLastVersion(id);

            setStatusAndVersion(AuditDict.STAGE_STAGE_SECOND, AuditDict.ROLE_INNER_OFFICE_MANAGER_CODE_ID, AuditDict.AUDIT_STATUS_NOT_PASS, AuditDict.CURRENT_STATUS_NOT_PASS, lastVersion);
            // 停止流程
            lastVersion.setIsActive(Boolean.FALSE);
            boolean b1 = iContestApplyRecordService.save(lastVersion);
            if (!b1) {
                log.warn("记录失败");
            } else {
                // 记录历史人员信息
                if (!recordPersonInfoHistory(id, lastVersion)) log.warn("记录历史人员信息失败");
            }

            setUpdateWrapper(wrapper, id, AuditDict.STAGE_STAGE_SECOND, AuditDict.ROLE_INNER_NO, AuditDict.AUDIT_STATUS_NO, AuditDict.CURRENT_STATUS_NOT_REPORT);

            // 防止跨级跨阶段审核
            super.handlerRequestPersonInfo(request);

            return iContestApplyActiveService.update(wrapper);

        }


        return Boolean.FALSE;

    }

    @Override
    public Boolean handlerRequestContestResult(OaRequest request) throws DtoNullException {

        if (!(request.getRoleCodeList().contains(super.role)))
            return super.handler.handlerRequestContestResult(request);

        Long id = request.getId();
        Integer auditStatus = request.getAuditStatus();
        UpdateWrapper<ContestApplyActiveModel> wrapper = new UpdateWrapper<>();

        if (AuditDict.AUDIT_STATUS_PASS.equals(auditStatus)) {

            ContestApplyRecordModel lastVersion = getLastVersion(id);
            setStatusAndVersion(AuditDict.STAGE_STAGE_THIRD, AuditDict.ROLE_INNER_OFFICE_MANAGER_CODE_ID, AuditDict.AUDIT_STATUS_PASS, AuditDict.CURRENT_STATUS_NOT_AUDIT, lastVersion);
            boolean b1 = iContestApplyRecordService.save(lastVersion);
            if (!b1) {
                log.warn("记录失败");
            } else {
                // 记录历史结果信息
                if (!recordResultInfoHistory(id, lastVersion)) log.warn("记录历史结果信息失败");
            }

            setUpdateWrapper(wrapper, id, AuditDict.STAGE_STAGE_THIRD, AuditDict.ROLE_INNER_OFFICE_CHARGE_CODE_ID, AuditDict.AUDIT_STATUS_NO, AuditDict.CURRENT_STATUS_NOT_AUDIT);

            // 防止跨级跨阶段审核
            super.handlerRequestContestResult(request);

            return iContestApplyActiveService.update(wrapper);

        } else if (AuditDict.AUDIT_STATUS_NOT_PASS.equals(auditStatus)) {

            ContestApplyRecordModel lastVersion = getLastVersion(id);

            setStatusAndVersion(AuditDict.STAGE_STAGE_THIRD, AuditDict.ROLE_INNER_OFFICE_MANAGER_CODE_ID, AuditDict.AUDIT_STATUS_NOT_PASS, AuditDict.CURRENT_STATUS_NOT_PASS, lastVersion);
            // 停止流程
            lastVersion.setIsActive(Boolean.FALSE);
            boolean b1 = iContestApplyRecordService.save(lastVersion);
            if (!b1) {
                log.warn("记录失败");
            } else {
                // 记录历史结果信息
                if (!recordResultInfoHistory(id, lastVersion)) log.warn("记录历史结果信息失败");
            }

            setUpdateWrapper(wrapper, id, AuditDict.STAGE_STAGE_THIRD, AuditDict.ROLE_INNER_NO, AuditDict.AUDIT_STATUS_NO, AuditDict.CURRENT_STATUS_NOT_REPORT);

            // 防止跨级跨阶段审核
            super.handlerRequestContestResult(request);

            return iContestApplyActiveService.update(wrapper);

        }


        return Boolean.FALSE;

    }
}
